import urlConfig from './config.js'

// const md5 = require('../static/utils/md5.min.js')

import md5 from '../static/utils/md5.min.js'

const request = {}
 

// 参数排序
function objKeySort(obj) {
	var newkey = Object.keys(obj).sort();
	var newObj = {};
	for (var i = 0; i < newkey.length; i++) {
			newObj[newkey[i]] = obj[newkey[i]];
	}
	return newObj;
}

function successState(res) {
  if(res.code){
    if(res.code == 200){
			return res
    }else if(res.code == 201){
			uni.showToast({
				icon: 'none',
				title: '登录过期，请重新登录!',
				duration: 1500
			})
			setTimeout(() => {
				uni.clearStorageSync('userInfo')
				uni.reLaunch({
					url: '/pages/login/login'
				})
			},1500)
		}else{
      return uni.showToast({
      	icon: 'none',
				title: res.error,
				duration: 1500
      })
    }
  }else{
		uni.showToast({
			icon: 'none',
			title: '接口报错!',
			duration: 1500
		})
  }
}

request.globalRequest = (url, method, data,loading) => {
	
	if(loading){
		uni.showLoading({
			mask: true,
			title: '加载中'
		})
	}
	
	if(!data){
		data = {}
	}
	
	// #ifdef H5
	let ua = navigator.userAgent.toLowerCase()
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		data.port = 'wechatH5Store'
	}else{
		data.port = 'h5Store'
	}
	// #endif
	
	// #ifdef MP-WEIXIN
	data.port = 'wechatStore'
	// #endif
	
	// #ifdef APP-PLUS
	switch(uni.getSystemInfoSync().platform){
		case 'android':
			data.port = 'androidStore'
		  break;
		case 'ios':
			data.port = 'iosStore'
		  break;
	}
	// #endif
	
	if(uni.getStorageSync('userInfo')){
		data.key = JSON.parse(uni.getStorageSync('userInfo')).token
	}
	
	// data.key = JSON.parse(uni.getStorageSync('userInfo')).token?JSON.parse(uni.getStorageSync('userInfo')).token:''
	
	data.timestamp = Math.round(new Date() * 1000)
	// 获取签名开始
	let obj = 'jjds2020+'
	let newObj = objKeySort(data)
	for (let i in newObj){
		obj += i + '=' + newObj[i] +'&'
	}
	obj = obj.substring(0, obj.length - 1)
	obj += '+jjds2020'
	console.log(obj)
	obj = md5(obj).toUpperCase()
	obj = obj.toUpperCase()
	// 获取签名结束
	data.sign = obj
	
	let promise = new Promise(function(resolve, reject) {
		uni.request({
			url: urlConfig + url,
			method,
			data: data,
			dataType: 'json',
			header: {
				'content-type':'application/x-www-form-urlencoded'
			}
		}).then(res => {
			uni.hideLoading()
			if(res[1].data.code==200){
				resolve(res[1].data)
			}else if(res[1].data.error!=='支付中'){
        successState(res[1].data)
      }else if(res[1].data.error==='支付中'){
				reject(res[1].data)
			}
		}).catch(res => {
			reject(res)
			uni.hideLoading()
			uni.getNetworkType({
				success(resNet) {
					if(resNet.networkType=='none'){
						uni.showToast({
							icon: 'none',
							title: '网络连接异常，请稍后重试！',
							duration: 1500
						})
					}else{
						if(res[1].data){
							reject(res[1].data)
						}else{
							uni.showToast({
								icon: 'none',
								title: '加载失败，请重试！',
								duration: 1500
							})
						}
					}
				}
			})
		})
	})
	return promise
}

export default request